package com.tarena.lbs.business.service.impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.param.StoreParam;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import com.tarena.lbs.business.pojo.vo.StoreVO;
import com.tarena.lbs.business.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreRepository storeRepository;

    @Override
    public void save(StoreParam param) throws BusinessException {
        log.info("新增店铺业务参数：{}", param);
        String storeName = param.getStoreName();
        int num = storeRepository.selectByStoreName(storeName);
        if (num > 0) {
            throw new BusinessException("-2","店铺名重复");
        }
        StorePO storePO = new StorePO();
        BeanUtils.copyProperties(param, storePO);
        storePO.setCreateTime(new Date());
        storePO.setStoreLogo("暂无logo");
        storeRepository.save(storePO);

    }

    @Override
    public void enable(Integer id) {
        log.info("启用店铺参数：{}", id);
        StorePO storePO = new StorePO();
        storePO.setId(id);
        storePO.setStoreStatus(0);
        storePO.setUpdateTime(new Date());
        storeRepository.updateStatus(storePO);
    }

    @Override
    public void disable(Integer id) {
        log.info("停用店铺参数：{}", id);
        StorePO storePO = new StorePO();
        storePO.setId(id);
        storePO.setStoreStatus(-1);
        storePO.setUpdateTime(new Date());
        storeRepository.updateStatus(storePO);
    }

    @Override
    public StoreVO detail(Integer id) {
        StoreVO storeVO = storeRepository.findById(id);
        return storeVO;
    }

    @Override
    public void delete(Integer id) {
        storeRepository.delete(id);
    }

    @Override
    public void updateStore(StoreParam param) {
        StorePO storePO = new StorePO();
        BeanUtils.copyProperties(param, storePO);
        storePO.setUpdateTime(new Date());
        storePO.setStoreLogo("暂无logo");
        storeRepository.updateStore(storePO);
    }

    public PageResult<StoreVO> pageList(StoreQuery query) {
        PageResult<StoreVO> pageResult = storeRepository.selectpageList(query);
        return pageResult;
    }
}
